package com.chezai.client.global.device;

import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;

import com.chezai.client.utils.DeviceUtils;

import java.lang.reflect.Method;

/**
 * 不同设备硬件模块接口
 * Created by LZF on 2016/11/5.
 */
public abstract class DifferenceHardware {
    public String TAG = this.getClass().getSimpleName();

    //获取设备序列号
    public abstract String getSerialNumber(Context context);

    //刷卡
    public abstract String readIcCard(Context context);

    //设置系统时间
    public abstract void setCalibrateTime();

    //打开系统设置页面
    public abstract void openSettings(Context context);

    //获取设备掉电时间
    public abstract long getPowerTime();

    //验证摄像头是否可用
    public abstract boolean cameraIsCanUse();

    //控制继电器开关 true 通
    public abstract boolean controlRelay(boolean open);

    //安装apk
    public abstract void installApk(Context context, String filePath);

    //设置acc关机倒计时时长
    public abstract void setAccPowerTime(Context context, long powerTime);

    //设置屏幕横屏方向
    public abstract void setScreenOrientation(Activity activity);

    //是否需要旋转图片 小鹿设备有旋转180度
    public abstract boolean isNeedRotaingImage();

    //禁用通知栏
    public abstract void disableNotificationBar(Context context);

    public abstract void rebootDevice(Context context);

    public abstract void closeSerialPort();

    //读取身份证 卡号
    public abstract void readIDCard(Context context);

//    public String getImei(Context context) {
//        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
//            return null;
//        }
//        String imei = telephonyManager.getDeviceId();
//        return imei;
//
//    }

    /**
     * 获取手机imei
     *
     * @param context
     * @return
     */
    //getIMEI(context,"gsm.mtk.imei","")；
//    public static String getImei(Context context, String prop, String defaultvalue) {
//        String imei = "";
//        imei = get(prop, defaultvalue);
//        if (imei.length() > 14) {
//            return imei;
//        } else {
//            TelephonyManager teleMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//            return teleMgr.getDeviceId();
//        }
//    }

    public static String getImei(Context context) {
        String imei = "";
        imei = get("gsm.mtk.imei", "");
        if (imei.length() > 14) {
            return imei;
        } else {
            TelephonyManager teleMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            return teleMgr.getDeviceId();
        }
    }
    private static volatile Method get = null;

    public static String get(String prop, String defaultvalue) {
        String value = defaultvalue;
        try {
            if (null == get) {
                synchronized (DeviceUtils.class) {
                    if (null == get) {
                        Class<?> cls = Class.forName("android.os.SystemProperties");
                        get = cls.getDeclaredMethod("get", new Class<?>[]{String.class, String.class});
                    }
                }
            }
            value = (String) (get.invoke(null, new Object[]{prop, defaultvalue}));
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return value;
    }
}
